#include "stm32f0_discovery.h"
#include "uart.h"


uint32_t msec_tick = 0;
volatile uint32_t uart_ready = 0;


/**
* @brief   		Initialize STM32F0 Discovery board - LED3, LED4, pushbutton
* @param  		None
* @retval 		None
*/
void Board_Init() {
	RCC_ClocksTypeDef RCC_Clocks;
	
	STM_EVAL_LEDInit(LED3);
	STM_EVAL_LEDInit(LED4);
	STM_EVAL_PBInit(BUTTON_USER, BUTTON_MODE_GPIO);
	
	// Configure SysTick for 1-msec interval 
  RCC_GetClocksFreq(&RCC_Clocks);
  SysTick_Config(RCC_Clocks.HCLK_Frequency / 1000);
}

/**
* @brief   		SysTick handler for delay function
* @param  		None
* @retval 		None
*/
void SysTick_Handler() {
	if (msec_tick > 0) {
		msec_tick--;
	}
}


/**
* @brief   		Delay function in millisecond
* @param  		None
* @retval 		None
*/
void DelayWait(uint32_t tick) {
  msec_tick = tick;
  while (msec_tick > 0);
}


/**
* @brief   		Hook function for UART RXNE Handler - toggle LED3
* @param  		None
* @retval 		None
*/
void UART_HookFunc_UART_IT_RXNE(uint32_t ch) {
   //uint32_t buffer[20];
   //scanf("%s", buffer); 
   UART_Write(ch);
	STM_EVAL_LEDToggle(LED3);
}


/**
* @brief   		Hook function for UART TXE Handler - toggle LED4
* @param  		None
* @retval 		None
*/
void UART_HookFunc_UART_IT_TXE() {
	STM_EVAL_LEDToggle(LED4);
	uart_ready = UART_Ready();	
}


/**
* @brief   		Main loop
* @param  		None
* @retval 		None
*/
int main() {
	int i;
	Board_Init();
	UART_Config(9600);
	
	while(1) {
		//for (i = 0; i < 30; i++) {
			//if (UART_Ready() == 0) {
				//uart_ready = 0;
				//while(uart_ready == 0);
			//}
			//UART_Write(i);
		//}
		//DelayWait(2000);
	}
}
